var apiHost = wx.getStorageSync("apiHost")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    expressNo: '',     //物流号
    orderNo: '',       //订单号
    userId: '',        //购买人id
    method: 'express', //发货方式
    expressCode: '',   //物流公司code
    expressList: [],   //物流公司列表
    pickList:[],
    pickName:'选择快递公司'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var orderNo = options.orderNo;
    var userId = wx.getStorageSync("userId");

    that.setData({
      orderNo: orderNo,
      userId: userId
    })
    that.initExpressList()
  },


  /**
   * 获取物流信息列表
   */
  initExpressList :function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    wx.request({
      url: apiHost + 'hpp/project/express/expressList',
      method: 'get',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if ("10000" == code) {
          var expressList = res.data.data;
          var pickList = that.data.pickList;
        
          for (var i = 0; i < expressList.length;i++){
            var name = expressList[i].expressCompany
            pickList.push(name)
          }

          that.setData({
            expressList: expressList,
            pickList: pickList
          });
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: false
          })
        }
        wx.hideLoading();
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: false
        })
      }
    })
  },
  /**
   * 快递公司选择
   */
  bindPickerChange:function(e){
    var that = this;
    var index = e.detail.value;
    var expressList = that.data.expressList;
    var expressCode = expressList[index].expressCompanyNo;
    var expressCompany = expressList[index].expressCompany;
    
    that.setData({
      expressCode: expressCode,
      pickName: expressCompany
    });
  },

  /**
   * 快递单号输入框
   */
  expressNoInput:function(e){
    var that = this;
    console.log(e)
    var expressNo = e.detail.value;
    that.setData({
      expressNo: expressNo
    })
  },

  /**
   * 校验 参数
   */
  checkDeliverGood:function(){
    var that = this;
    var userId = that.data.userId;
    var orderNo = that.data.orderNo;
    var expressCode = that.data.expressCode;
    var expressNo = that.data.expressNo;
    var method = that.data.method;

    if (expressCode==''){
      that.promptMsg("请选择物流公司");
      return
    }

    if (expressNo == '') {
      that.promptMsg("请输入快递单号")
      return
    }
    that.deliverGood(userId, orderNo, expressCode, expressNo, method);
  },

  /**
   * 确认发货
   */
  deliverGood: function (userId, orderNo, expressCode, expressNo, method){
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    wx.request({
      url: apiHost + 'hpp/order/deliverGood',
      method: 'get',
      header: {
        'content-type': 'application/json'
      },
      data:{
        userId: userId,
        orderNo: orderNo,
        expressCode: expressCode,
        expressNo: expressNo,
        method: method
      },
      success: function (res) {
        console.log(res)
        var code = res.data.code;
        var msg = res.data.msg;
        if ("10000" == code) {
          that.goWxml("../hearOrderList/hearOrderList");    //跳转订单列表
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: false
          })
        }
        wx.hideLoading();
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: false
        })
      }
    })
  },
  /**
   * 提示信息
   */
  promptMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },
  /**
  * 跳转 页面
  */
  goWxml: function (url) {
    var that = this;
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });

    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})